using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GenePool: MonoBehaviour
{
	public List<GameObject> Attack;
	public List<GameObject> Intelligence;
	public List<GameObject> Defense;
	public List<GameObject> Speed;
	public List<GameObject> Strength;
	public static GenePool me;
	
	public void Awake() {
		me= this;
	}
	
	public static List<GameObject> GetPhenotype(Chromosome.ChromosomeType type, int max) {
		switch(type) {
			case Chromosome.ChromosomeType.ATTACK:
				int levels= AlienMating.MaxPower/me.Attack.Count; //divide the max power by the available physical options for "power levels" of objects 
				float physNum= Mathf.Floor(max/(float)levels);//find the whole number to indicate how many physical options you get
				return me.GetTraits(levels, physNum, me.Attack, max);
			break;
			case Chromosome.ChromosomeType.SMARTS:
				levels= AlienMating.MaxPower/me.Intelligence.Count; //divide the max power by the available physical options for "power levels" of objects 
				physNum= Mathf.Floor(max/(float)levels);//find the whole number to indicate how many physical options you get
				return me.GetTraits(levels, physNum, me.Intelligence, max);
			break;
			case Chromosome.ChromosomeType.DEFENSE:
				levels= AlienMating.MaxPower/me.Defense.Count; //divide the max power by the available physical options for "power levels" of objects 
				physNum= Mathf.Floor(max/(float)levels);//find the whole number to indicate how many physical options you get
				return me.GetTraits(levels, physNum, me.Defense, max);
			break;
			case Chromosome.ChromosomeType.SPEED:
				levels= AlienMating.MaxPower/me.Speed.Count; //divide the max power by the available physical options for "power levels" of objects 
				physNum= Mathf.Floor(max/(float)levels);//find the whole number to indicate how many physical options you get
				return me.GetTraits(levels, physNum, me.Speed, max);
			break;
			case Chromosome.ChromosomeType.STRENGTH:
				int pick= Random.Range(0, me.Strength.Count);
			
			
				List<GameObject> list= new List<GameObject>();
			if(me.Strength[pick] != null) {
				GameObject g= (GameObject)Instantiate(me.Strength[pick]);
				list.Add(g);
			
			}

				return list;
			break;
			default:
				return null;
			break;
		}
	}
	
	private List<GameObject> GetTraits(int levels, float physNum, List<GameObject> typeList, int max) {
//		Debug.Log("levels: "+levels+" physNum: "+physNum+" typeList: "+typeList+" max: "+max);
		List<GameObject> phenotype= new List<GameObject>();
		int rand= 0;
		for(int i =0; i< physNum; i++) {
			while(phenotype.Count != i) {
				rand= Random.Range(0, typeList.Count); //get a random number
				if (!phenotype.Contains(typeList[rand]) && typeList[rand] != null) {
					GameObject g= (GameObject)Instantiate(typeList[rand]);
					phenotype.Add(g);//randomly get an object
				}
				else if (typeList[rand] == null) {
					Debug.LogError("MISSING Type Index: " + rand);
				}
			}
			//scale the object based on max value
			int maxScale= i*levels; //max scale value
			float toScale= 0f;
			if(typeList[rand] != null) {
				if (max >= maxScale) typeList[rand].transform.localScale= new Vector3(1.5f,1.5f,1.5f); //if max is greater than or equal to above, then you need to scale this succer up
				else {//if max is less, how much less?
					float newScale= (levels-max)*.5f;
					typeList[rand].transform.localScale= new Vector3(newScale, newScale, newScale);//subtract levels-max, should give you a number, multiply by .5
				}
			}
		}
		return phenotype;
	}
	
	public static List<GameObject> CombinePhenotypes(Chromosome pA, Chromosome pB) {
		//based on each parent's max, randomly choose which max to keep if they aren't equal
		int newCount= 0;
		if (pA.Phenotype.Count!= pB.Phenotype.Count) { //we don't have the same number of assets
			List<int> countChoice= new List<int>();
			countChoice.Add(pA.Phenotype.Count);
			countChoice.Add(pB.Phenotype.Count);
			int randCount= Random.Range(0, countChoice.Count);
			newCount= countChoice[randCount];
		}
		else {
			newCount= pA.Phenotype.Count;	
		}
		List<GameObject> oldPhenotypesCombined= new List<GameObject>();
		foreach(GameObject g in pA.Phenotype) {
			oldPhenotypesCombined.Add(g);
		}
		foreach(GameObject g in pB.Phenotype) {
			oldPhenotypesCombined.Add(g);
		}
		//now that you know how many you need, simply pick randomly from a combined list of the parents
		List<GameObject> newPhenotype= new List<GameObject>();
		while (newPhenotype.Count != newCount) {
			int rand= Random.Range(0, oldPhenotypesCombined.Count);
			if (!newPhenotype.Contains(oldPhenotypesCombined[rand])) { 
				GameObject copy= (GameObject)Instantiate(oldPhenotypesCombined[rand]);
				copy.transform.localScale= Vector3.one;
				newPhenotype.Add(copy);
			}
		}
		//return
		return newPhenotype;
	}
}

